package springcloud.helloworld.ribbon.client;

import com.alibaba.fastjson.JSONObject;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Service
public class HelloService {
    @Autowired RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "serviceFailure",
            commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")})
    public String getHelloContent() {
    	System.out.println("call SERVICE-HELLOWORLD");
        return restTemplate.getForObject("http://SERVICE-HELLOWORLD/",String.class);
    }

    public String serviceFailure() {
        return "hello world service is not available !";
    }


//    @HystrixCommand(fallbackMethod = "serviceFailure2")
    @HystrixCommand(fallbackMethod = "serviceFailure2",commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "30000")})
    public String getTestContent(String str) {
        System.out.println("call SERVICE-HELLOWORLD test");

        Map<String, String> params = new HashMap<String, String>();
        params.put("abc", str);

        return restTemplate.getForObject("http://SERVICE-HELLOWORLD/test?str={abc}", String.class, params);
    }

    //带参数
    //主方法有参数，回调函数就得对应参数
    public String serviceFailure2(String str) {
        System.out.println("str=" + str);
        return "test service is not available !";
    }

    @HystrixCommand(fallbackMethod = "serviceFailure3",commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "30000")})
    public String getPostTestContent(String str) {
        System.out.println("call SERVICE-HELLOWORLD test for post");

        String url = "http://SERVICE-HELLOWORLD/testForPost";

        MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
        requestBody.add("str", str);

        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("api-version", "1.0");

        HttpEntity<MultiValueMap> requestEntity = new HttpEntity<MultiValueMap>(requestBody, requestHeaders);


        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, requestEntity, String.class);

        System.out.println(responseEntity.getBody());

//        return restTemplate.getForObject("http://SERVICE-HELLOWORLD/test?str={abc}", String.class, params);

        return responseEntity.getBody();
    }

    //带参数
    //主方法有参数，回调函数就得对应参数
    public String serviceFailure3(String str) {
        System.out.println("str=" + str);
        return "test service is not available !";
    }


}